package com.abel.demo.controller;

import com.abel.demo.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Author: Abel.lin
 * Date: Created in 2018/5/31 15:49
 * Copyright: Copyright (c) 2017
 * Description:
 */
@Slf4j
@RestController
public class IndexController {
    @Autowired
    private AsyncService asyncService;

    @RequestMapping("index")
    public String index(@RequestParam("n")Integer n){
        for (int i=0;i<n;i++){
            asyncService.test1(i);

        }
        return "hello word " + n;
    }

    @RequestMapping("future")
    public String future(@RequestParam("n")Integer n) throws ExecutionException, InterruptedException {
        List<Future<String>> list = new ArrayList<>();
        for (int i=0;i<n;i++){
            Future<String> future = asyncService.test2(i);
            list.add(future);
        }
        log.info("future start");
        for (Future<String> future : list) {
            log.info("future[{}]", future.get());
        }
        log.info("future end");
        return "hello word " + n;
    }


}
